package com.zmwx.subject.domain.handler.subject;

import com.alibaba.fastjson.JSON;
import com.zmwx.subject.common.enums.IsDeletedFlagEnum;
import com.zmwx.subject.common.enums.SubjectInfoTypeEnum;
import com.zmwx.subject.domain.convert.RadioSubjectConverter;
import com.zmwx.subject.domain.entity.SubjectAnswerBO;
import com.zmwx.subject.domain.entity.SubjectInfoBO;
import com.zmwx.subject.domain.entity.SubjectOptionBO;
import com.zmwx.subject.infra.basic.entity.SubjectMultiple;
import com.zmwx.subject.infra.basic.entity.SubjectRadio;
import com.zmwx.subject.infra.basic.service.SubjectRadioService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * 单选的题目策略类
 */
@Slf4j
@Component
public class RadioTypeHandler implements SubjectTypeHandler{

    @Resource
    SubjectRadioService subjectRadioService;
    @Override
    public SubjectInfoTypeEnum getHandlerType() {

        return SubjectInfoTypeEnum.RADIO;
    }

    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        log.info("单选题目的插入,单选题目策略类:{}", JSON.toJSONString(subjectInfoBO));
        //单选题目的插入
        List<SubjectRadio> subjectRadioList = new LinkedList<>();
        subjectInfoBO.getOptionList().forEach(option  ->{
            SubjectRadio subjectRadio = RadioSubjectConverter.INSTANCE.convertBoToEntity(option);
            subjectRadio.setSubjectId(subjectInfoBO.getId());
            subjectRadio.setIsDeleted(IsDeletedFlagEnum.DELETED.getCode());
            subjectRadioList.add(subjectRadio);
        });
        log.info("单选题目的插入.subjectRadio,:{}", JSON.toJSONString(subjectRadioList));

        subjectRadioService.batchInsert(subjectRadioList);
    }

    //题目详情
    @Override
    public SubjectOptionBO query(int subjectId) {
        SubjectRadio subjectRadio=new SubjectRadio();
        subjectRadio.setSubjectId(Long.valueOf(subjectId));
        List<SubjectRadio> result=  subjectRadioService.queryByCondition(subjectRadio);
        List<SubjectAnswerBO> subjectAnswerBOList=RadioSubjectConverter.INSTANCE.convertBoToSubjectAnswerBO(result);
        SubjectOptionBO subjectOptionBO=new SubjectOptionBO();
        subjectOptionBO.setOptionList(subjectAnswerBOList);
        return subjectOptionBO;
    }
}
